package io.renren.modules.app.controller;

import io.renren.common.utils.MapUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.service.entity.ProductEntity;
import io.renren.modules.service.service.ProductService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


/**
 * 产品表
 */
@RestController
@RequestMapping("/app/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = productService.queryPage(params);

        return R.ok().put("data", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        ProductEntity product = productService.getById(id);

        return R.ok().put("data", product);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(String productList) {
        if (StringUtils.isBlank(productList)) {
            return R.error("参数不能为空");
        }
        String productName = productService.insert(productList);

        return R.ok().put("data", new MapUtils().put("productName", productName));
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(ProductEntity product) {
        ValidatorUtils.validateEntity(product, UpdateGroup.class);
        productService.updateById(product);

        return R.ok();
    }

}
